<?php
class Privilege extends Model{

	public function getprivilege() {
		$sql = "select * from ".$this->db->prefix."privilegecate order by show_nav desc,ordernum asc";
		$pcarr = $this->db->getAll($sql);
		foreach($pcarr as $key=>$value){
			$sql = "select * from ".$this->db->prefix."privilege where pc_id = '".intval($value['pc_id'])."' order by show_nav desc,ordernum asc,p_action asc";
			$pcarr[$key]['parr']=$this->db->getAll($sql);
		}
		return $pcarr;
	}

	public function getprivilegecate() {
		$sql = "select * from ".$this->db->prefix."privilegecate";
		return $this->db->getAll($sql);
	}
	public function getonepriviege() {
		if(isset($_GET['pid'])){
			$sql = "select * from ".$this->db->prefix."privilege where p_id = '".intval($_GET['pid'])."'";
			return $this->db->getRow($sql);
		}else{
			$sql = "select * from ".$this->db->prefix."privilegecate where pc_id = '".intval($_GET['pcid'])."'";
			return $this->db->getRow($sql);
		}
	}
	public function getroleprivilege($rid) {
		$act = strtolower($this->router->controller.'/'.$this->router->action);
		$sql = "select * from ".$this->db->prefix."privilegerole pr left join ".$this->db->prefix."privilege p on pr.p_id=p.p_id where pr.role_id = '".intval($rid)."' and lower(p.p_action) = '$act'";
		$result = $this->db->query($sql);
		$num = $this->db->num_rows($result);
		if($num){
			return false;
		}
		return true;
	}
	public function addprivilegecate() {
		$table = $this->db->prefix.'privilegecate';
		foreach($_POST['pc_name'] as $key=>$value){
			$value = trim($value);
			if(!empty($value)){
				$arr = array('pc_name'  =>$_POST['pc_name'][$key],
							 'show_nav' =>$_POST['show_nav'][$key],
							 'ordernum' =>$_POST['ordernum'][$key]
				);
				$this->db->insert($table, $arr);
			}
		}
	}

	public function updateprivilege(){
		$show_nav = isset($_POST['show_nav'])?$_POST['show_nav']:0;
		if(isset($_POST['p_id'])){
			$table = $this->db->prefix.'privilege';
			$wherestr = "p_id='".intval($_POST['p_id'])."'";
			$arr = array('pc_id'=>$_POST['pc_id'],
						 'p_name'=>$_POST['p_name'],
						 'p_action'=>$_POST['p_action'],
						 'show_nav'=>$show_nav,
						 'ordernum' =>$_POST['ordernum']
			);
			$this->db->update($table, $arr,$wherestr);
		}else{
			$table = $this->db->prefix.'privilegecate';
			$wherestr = "pc_id='".intval($_POST['pc_id'])."'";
			$arr = array('pc_name'=>$_POST['pc_name'],
						 'show_nav'=>$show_nav,
						 'ordernum' =>$_POST['ordernum']
			);
			$this->db->update($table, $arr,$wherestr);
		}
	}

	public function delprivilege(){
		if(isset($_GET['pid'])){
			$table = $this->db->prefix.'privilege';
			$this->db->delete($table, 'p_id',$_GET['pid']);
		}else{
			$table = $this->db->prefix.'privilegecate';
			$this->db->delete($table, 'pc_id',$_GET['pcid']);
		}
	}
	public function addprivilege() {
		$table = $this->db->prefix.'privilege';
		foreach($_POST['p_name'] as $key=>$value){
			$value = trim($value);
			if(!empty($value)){
				$arr = array('pc_id'    =>intval($_POST['pc_id']),
							 'p_name'  =>$_POST['p_name'][$key],
							 'p_action'=>$_POST['p_action'][$key],
							 'show_nav' =>$_POST['show_nav'][$key],
							 'ordernum' =>$_POST['ordernum'][$key]
				);
				$this->db->insert($table, $arr);
			}
		}
	}
}